unit Form_Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, frxDesgn, frxClass,frxPreview,frxPreviewPages,frxXML,
  FormEx_View,Dialog_View;

type
  TPrintData=class(TObject)
  public
    PrintIdex:Integer;
    PrintName:string;
    PrintDemo:string;
    PrintCode:string;
    PrintText:string;
  end;

  TFormMain = class(TDialogView)
    frxReport1: TfrxReport;
    frxDesigner1: TfrxDesigner;
    Btnx_1: TButton;
    Btnx_2: TButton;
    Btnx_3: TButton;
    DataSet_1: TfrxUserDataSet;
    procedure Btnx_1Click(Sender: TObject);
    procedure Btnx_2Click(Sender: TObject);
    procedure frxReport1GetValue(const VarName: String;
      var Value: Variant);
    procedure Btnx_3Click(Sender: TObject);
    procedure DataSet_1GetValue(const VarName: String; var Value: Variant);
  private
    FListData:TStringList;
  protected
    procedure SetInitialize;override;
    procedure SetCommParams;override;
    procedure SetGridParams;override;
    procedure SetComboItems;override;
    procedure TryFreeAndNil;override;
  public
    procedure InitData;
  end;

var
  FormMain: TFormMain;

implementation

uses
  Class_KzUtils,Class_KzPrint,Class_KzDebug;

{$R *.dfm}

procedure TFormMain.Btnx_1Click(Sender: TObject);
var
  I:Integer;
  PageA:TfrxReportPage;
  StrmA:TFileStream;
begin
  if not FileExists('print0001.fr3') then
  begin
    frxReport1.SaveToFile('print0001.fr3');
  end;

  frxReport1.LoadFromFile('print0001.fr3');

  DataSet_1.RangeEnd := reCount;
  DataSet_1.RangeEndCount:=FListData.Count;

  frxReport1.PrepareReport();

  with frxReport1 do
  begin
    for I:=0 to PreviewPages.Count-1 do
    begin
      PageA:=TfrxReportPage(PreviewPages.Page[I]);
      StrmA:=TFileStream.Create(Format('page%D.xml',[I]),fmCreate);
      PageA.SaveToStream(StrmA);
      FreeAndNil(StrmA);
    end;
  end;
  frxReport1.ShowPreparedReport;
end;

procedure TFormMain.Btnx_2Click(Sender: TObject);
begin
  frxReport1.LoadFromFile('print0001.fr3');
  frxReport1.DesignReport();
end;

procedure TFormMain.frxReport1GetValue(const VarName: String;
  var Value: Variant);
begin
  if VarName='variable00001' then Value:='kazarus';
  if VarName='variable00002' then Value:='kazarus.report';
end;

procedure TFormMain.Btnx_3Click(Sender: TObject);
var
  I:Integer;
  PageA:TfrxReportPage;
  StrmA:TFileStream;
  StrmC:TFileStream;  
  StrmB:TMemoryStream;
  ListA:TStringList;

  frxXML:TfrxXMLDocument;
  frxDoc:TfrxXMLItem;
  frxNod:TfrxXMLItem;

  function  DoRead(AfrxNod:TfrxXMLItem):TfrxXMLItem;
  var
    X:Integer;
  begin
    KzDebug.FileFmt('%S:%S',[AfrxNod.Name,AfrxNod.Text]);

    if AfrxNod.Name='TfrxMemoView' then
    begin
      if AfrxNod.Prop['Name']='frxUserDataSet1PrintName' then
      begin
        if AfrxNod.Prop['Text']='PrintName1' then
        begin
          AfrxNod.Prop['Font.Color']:=IntToStr(ColorToRGB(clGreen));
        end;
      end;
    end;  
    {if AfrxNod.PropExists('Font.Color') then
    begin
      AfrxNod.Prop['Font.Color']:=IntToStr(ColorToRGB(clGreen));
    end;}
    if AfrxNod.Count>0 then
    begin
      for X:=0 to AfrxNod.Count-1 do
      begin
        DoRead(AfrxNod.Items[X]);
      end;  
    end;  
  end;  
begin
  ListA:=TStringList.Create;
  
  frxReport1.Clear;
  frxReport1.PreviewPages.Clear;
    
  StrmA:=TFileStream.Create('page0.xml',fmOpenReadWrite);

  try
    frxXML:=TfrxXMLDocument.Create;
    frxXML.Clear;    
    frxXML.AutoIndent:=True;

    StrmA.Position:=0;
    frxXML.LoadFromStream(StrmA);

    frxDoc:=frxXML.Root;
    DoRead(frxDoc);

    StrmA.Position:=0;
    frxXML.SaveToStream(StrmA);

    StrmB:=TMemoryStream.Create;
    StrmB.Position:=0;
    StrmB.LoadFromStream(StrmA);
  finally
    FreeAndNil(frxXML);
  end;

  PageA:=TfrxReportPage.Create(nil);

  //StrmB.Position:=0;
  //PageA.LoadFromStream(StrmB);

  
  StrmA.Position:=0;
  PageA.LoadFromStream(StrmA);

  {StrmC:=TFileStream.Create('modify.xml',fmCreate);
  PageA.SaveToStream(StrmC);}

  frxReport1.PreviewPages.AddPage(PageA);
  frxReport1.PreviewPages.ModifyPage(0,PageA);
  {frxReport1.PreviewPages.AddSourcePage(PageA);}
  ListA.AddObject('',PageA);


  FreeAndNil(StrmA);


  StrmA:=TFileStream.Create('page1.xml',fmOpenRead);
  PageA:=TfrxReportPage.Create(nil);
  PageA.LoadFromStream(StrmA);
  frxReport1.PreviewPages.AddPage(PageA);
  ListA.AddObject('',PageA);  
  frxReport1.PreviewPages.ModifyPage(1,PageA);
  FreeAndNil(StrmA);

  
  //YXC_2013_11_01_15_44_40_<
  with frxReport1 do
  begin
    for I:=0 to PreviewPages.Count-1 do
    begin
      PageA:=TfrxReportPage(PreviewPages.Page[I]);
      StrmA:=TFileStream.Create(Format('page_after%D.xml',[I]),fmCreate);
      PageA.SaveToStream(StrmA);
      FreeAndNil(StrmA);
    end;
  end;  
  //YXC_2013_11_01_15_44_40_>
  frxReport1.ShowPreparedReport;
  //frxReport1.ShowReport();


  if ListA<>nil then TKzUtils.TryFreeAndNil(ListA);
  if StrmB<>nil then TKzUtils.TryFreeAndNil(StrmB);
end;

procedure TFormMain.SetComboItems;
begin
  inherited;

end;

procedure TFormMain.SetCommParams;
begin
  inherited;

end;

procedure TFormMain.SetGridParams;
begin
  inherited;

end;

procedure TFormMain.SetInitialize;
begin
  inherited;
  InitData;
end;

procedure TFormMain.TryFreeAndNil;
begin
  inherited;
  if FListData<>nil then TKzUtils.TryFreeAndNil(FListData);
end;

procedure TFormMain.InitData;
var
  I:Integer;
  DataA:TPrintData;
begin
  if FListData<>nil then TKzUtils.TryFreeAndNil(FListData);
  FListData:=TStringList.Create;
  
  for I:=0 to 20 do
  begin
    DataA:=TPrintData.Create;

    DataA.PrintIdex:=I+1;
    DataA.PrintName:=Format('PrintName%D',[I]);
    DataA.PrintDemo:=Format('PrintDemo%D',[I]);
    DataA.PrintCode:=Format('PrintCode%D',[I]);
    DataA.PrintText:=Format('PrintText%D',[I]);

    FListData.AddObject('',DataA);
  end;  
end;

procedure TFormMain.DataSet_1GetValue(const VarName: String;
  var Value: Variant);
var
  DataA:TPrintData;
begin
  DataA:=TPrintData(FListData.Objects[DataSet_1.RecNO]);
  if DataA<>nil then
  begin
    if VarName='PrintIdex' then Value:=DataA.PrintIDex;
    if VarName='PrintName' then Value:=DataA.PrintName;
    if VarName='PrintDemo' then Value:=DataA.PrintDemo;
    if VarName='PrintCode' then Value:=DataA.PrintCode;
    if VarName='PrintText' then Value:=DataA.PrintText;                
  end;
end;

end.
